import Vue from "vue";
import axios from "axios";
import router from "../router"
import { Toast } from 'vant';



//判断环境
//开发环境 8080
if (process.env.NODE_ENV === "development") {
    Vue.prototype.$pre = "http://localhost:3000"
  } else {
    //生产环境 3000
    Vue.prototype.$pre = ""
  }


  // 
axios.interceptors.request.use((req)=>{
  // console.log(req);
    // 如果请求的地址不是登录也不是注册，就得携带请求头
    if(req.url != "/api/login" && req.url != "/api/register"){
        req.headers.authorization = JSON.parse(localStorage.getItem("userInfo")).token;
    }
    // if(req.url == "/api/addresslist" || req.url == "/api/addressadd" || req.url == "/api/addressedit" || req.url == "/api/addressremove"){
    //   req.headers[ContentType] = "application/json";
    // }
    return req;
  });
  
  axios.interceptors.response.use((res)=>{
    //统一处理一下失败
    if (res.data.code !== 200) {
        Toast(res.data.msg)
    }
  
    //统一处理掉线
    if (res.data.msg === "登录已过期或访问权限受限") {
        //跳转到登录页
        router.replace("/login")
    }
  
    //将后端返回过来的数据给组件
    return res;
  })



